using System;

namespace Esst.Dto
{
    /// <summary>
    /// <para>分页信息</para>
    /// </summary>
    [Serializable]
    public class PageInfo
    {
        private int currentPageIndex;
        private int pageSize;
        private int _recordCount;

        /// <summary>
        /// 记录数
        /// </summary>
        public int RecordCount { get => _recordCount; set => _recordCount = value < 0 ? 0 : value; }

        /// <summary>
        /// 页数
        /// </summary>
        public int PageCount { get => Convert.ToInt32(Math.Ceiling(Convert.ToDecimal(_recordCount) / pageSize)); }

        /// <summary>
        /// 最后一页记录数
        /// </summary>
        public int RecordsInLastPage
        {
            get
            {
                if (_recordCount == 0)
                    return 0;

                var t = _recordCount % pageSize;
                return t == 0 ? pageSize : t;
            }
        }

        /// <summary>
        /// 页大小(不得小于1)
        /// </summary>
        public int PageSize { get => pageSize; set => pageSize = value < 1 ? 1 : value; }

        /// <summary>
        /// 当前页码(从0开始)
        /// </summary>
        public int CurrentPageIndex { get => currentPageIndex; set => currentPageIndex = value < 0 ? 0 : value; }

        /// <summary>
        /// 是否分页：false表示仅按照所给的字段进行排序操作
        /// </summary>
        public bool IsPage { get; set; }

        /// <summary>
        /// <para>是否取前几行：</para>
        /// <para>仅当IsPage为false时生效，</para>
        /// <para>取出当前排序的前几个数据，</para>
        /// <para>数目由PageSize决定</para>
        /// </summary>
        public bool IsSelectTop { get; set; }

        /// <summary>
        /// 第一个排序字段
        /// </summary>
        public string SortField1 { get; set; }

        /// <summary>
        /// 第一个排序字段排序方法
        /// </summary>
        public SortType SortType1 { get; set; }

        /// <summary>
        /// 第二个排序字段
        /// </summary>
        public string SortField2 { get; set; }

        /// <summary>
        /// 第二个排序字段排序方法
        /// </summary>
        public SortType SortType2 { get; set; }

        /// <summary>
        /// 第三个排序字段
        /// </summary>
        public string SortField3 { get; set; }

        /// <summary>
        /// 第三个排序字段排序方法
        /// </summary>
        public SortType SortType3 { get; set; }

        /// <summary>
        ///
        /// </summary>
        public PageInfo()
        {
            IsSelectTop = false;
            IsPage = true;
            currentPageIndex = 0;
            pageSize = 10;
            RecordCount = 0;
            SortField1 = null;
            SortType1 = SortType.ASC;
            SortField2 = null;
            SortType2 = SortType.ASC;
            SortField3 = null;
            SortType3 = SortType.ASC;
        }

        /// <summary>
        /// 排序方式
        /// </summary>
        public enum SortType
        {
            /// <summary>
            /// 正序
            /// </summary>
            ASC,

            /// <summary>
            /// 逆序
            /// </summary>
            DESC
        }

        public PageInfo Copy()
        {
            return MemberwiseClone() as PageInfo;
        }
    }
}